package com.yixin.hospital.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.domain.Guidance;
import com.yixin.hospital.entity.domain.NextVisit;
import com.yixin.hospital.entity.domain.VisitInspectionList;
import com.yixin.hospital.entity.dto.req.nextvisit.AddNextVisitReqDTO;
import com.yixin.hospital.entity.vo.NextVisitVO;
import com.yixin.hospital.mapper.NextVisitMapper;
import com.yixin.hospital.service.GuidanceService;
import com.yixin.hospital.service.NextVisitService;
import com.yixin.hospital.service.VisitInspectionListService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
* @author 
* @description 针对表【next_visit】的数据库操作Service实现
* @createDate 2024-11-10 16:13:05
*/
@Service
public class NextVisitServiceImpl extends ServiceImpl<NextVisitMapper, NextVisit>
    implements NextVisitService{

    @Resource
    private GuidanceService guidanceService;
    @Resource
    VisitInspectionListService visitInspectionListService;


    @Override
    public ApiResult<?> add(AddNextVisitReqDTO addNextVisitReqDTO, String userName) {
        NextVisit nextVisit = this.getOne(new LambdaQueryWrapper<NextVisit>().eq(NextVisit::getGuidanceCode, addNextVisitReqDTO.getGuidanceCode()));
        if (nextVisit != null) {
            this.removeById(nextVisit.getId());
        }
        nextVisit = new NextVisit();
        nextVisit.setGuidanceCode(addNextVisitReqDTO.getGuidanceCode());
        nextVisit.setNextVisitDailyList(JSON.toJSONString(addNextVisitReqDTO.getNextVisitDailyList()));
        nextVisit.setNextVisitList(JSON.toJSONString(addNextVisitReqDTO.getNextVisitList()));
        nextVisit.setNextVisitRemark(addNextVisitReqDTO.getNextVisitRemark());
        nextVisit.setCreatedBy(userName);
        nextVisit.setUpdatedBy(userName);
        this.save(nextVisit);
        return ApiResult.success();
    }

    @Override
    public ApiResult<?> getUserLastNextVisit(Long userId) {
        Guidance guidance = guidanceService.getOne(new LambdaQueryWrapper<Guidance>().eq(Guidance::getPatientId, userId).orderByDesc(Guidance::getCreatedTime).last("limit 1 OFFSET  1"));
        if (guidance == null) {
            return ApiResult.success();
        }
        NextVisitVO nextVisitVO = new NextVisitVO();
        NextVisit nextVisit = this.getOne(new LambdaQueryWrapper<NextVisit>().eq(NextVisit::getGuidanceCode, guidance.getGuidanceCode()));
        if (Objects.isNull(nextVisit)) return ApiResult.success(nextVisitVO);
        BeanUtils.copyProperties(nextVisit, nextVisitVO);
        String nextVisitList = nextVisit.getNextVisitList();
        JSONArray jsonArray = JSONArray.parseArray(nextVisitList);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject o = JSONObject.parseObject(jsonArray.get(i).toString());
            String code = o.get("code").toString();
            VisitInspectionList visitInspectionList = visitInspectionListService.getOne(new LambdaQueryWrapper<VisitInspectionList>().eq(VisitInspectionList::getCode, code).eq(VisitInspectionList::getStatus, 2).orderByDesc(VisitInspectionList::getCreatedTime).last("limit 1"));
            nextVisitVO.setLastTime(visitInspectionList.getCreatedTime());
        }
        return ApiResult.success(nextVisitVO);
    }
}




